package com.tjsinfo.tjsoa.system.action;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;

/**
 * 查询系统用户
 * @author 王晓伟
 *
 */
public class QiyongUserAction extends Action{
	private ISystemUserBo systemUserBo;

	public ISystemUserBo getSystemUserBo() {
		return systemUserBo;
	}

	public void setSystemUserBo(ISystemUserBo systemUserBo) {
		this.systemUserBo = systemUserBo;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		List<TSystemUser> list = null;
		Integer start = 0;
		JSONObject totalproperty = new JSONObject();
		String username = request.getParameter("userName");
		if (username == null) {
			if(request.getParameter("start") == null)
			{
				list = systemUserBo.selectQiyongSystemUser(0, 15,(byte) 0);
			}
			else
			{
				start = Integer.parseInt(request.getParameter("start"));
				list = systemUserBo.selectQiyongSystemUser(start, 15,(byte) 1);
			}
			totalproperty.put("totalProperty",systemUserBo.selectSytemUserTotalLine());
		} else {
			if(request.getParameter("start") == null)
			{
				list = systemUserBo.selectSystemUserByLikeUsername(username, 0, 15);
			}
			else
			{
				start = Integer.parseInt(request.getParameter("start"));
				list = systemUserBo.selectSystemUserByLikeUsername(username, start, 15);
			}
			totalproperty.put("totalProperty",systemUserBo.selectSytemUserLikeUsernameTotalLine(username));
		}
		
		
		JSONArray array = new JSONArray();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			TSystemUser systemUser = (TSystemUser) iterator.next();
			JSONObject systemobject = new JSONObject();
			systemobject.put("orderNo", systemUser.getOrderNo());
			systemobject.put("loginName", systemUser.getLoginName());
			systemobject.put("userName", systemUser.getUserName());
			systemobject.put("email", systemUser.getEmail());
			if (systemUser.getState() == 1) {
				systemobject.put("state", "禁用");
				systemobject.put("operate", "<a href='javascript:enableSystemUser("+systemUser.getUserId()+")'>启用</a>&nbsp;&nbsp;&nbsp;<a href='"+basePath+"tjsinfo/tjsoa/system/selectUserById.do?id="+systemUser.getUserId()+"&start="+start+"'>修改</a>&nbsp;&nbsp;&nbsp;<a href='javascript:resetPassWord("+systemUser.getUserId()+")'>重置密码</a>");
			} else {
				systemobject.put("state", "启用");
				systemobject.put("operate", "<a href='javascript:enableSystemUser("+systemUser.getUserId()+")'>禁用</a>&nbsp;&nbsp;&nbsp;<a href='"+basePath+"tjsinfo/tjsoa/system/selectUserById.do?id="+systemUser.getUserId()+"&start="+start+"'>修改</a>&nbsp;&nbsp;&nbsp;<a href='javascript:resetPassWord("+systemUser.getUserId()+")'>重置密码</a>");
			}
			
			systemobject.put("userId", systemUser.getUserId());
			systemobject.put("uuid", systemUser.getUuid());
		
			array.put(systemobject);
		}
		HttpSession session = request.getSession();
		session.setAttribute("qiyongliststart", start);
		totalproperty.put("root", array);
		response.getWriter().print(totalproperty);
		return null;
	}
	
}
